#!/usr/bin/perl

use strict;
use warnings;

my $dir = '/mnt/cscbNAS/Data';
opendir DIR, $dir or die "cannot open directory $dir $!\n";
while(my $file = readdir DIR){
    if($file =~ m/(.+)[_-](\d{4}-\d{2}-\d{2})/){
	my $newFile = "$2_$1";
	print "$file\t$newFile\n";
#	rename "$dir/$file", "$dir/$newFile" or die "rename failed $!\n";
    }
    if($file =~ m/(.+)[-_](\d{2})(\d{2})(\d{2})/){
	my $newFile = "20$4-$2-$3_$1";
	print "$file\t$newFile\n";
#	rename "$dir/$file", "$dir/$newFile" or die "rename failed $!\n";
    }
    if($file =~ m/(\d{2})(\d{2})(\d{2})[_-](.+)/){
	my $newFile = "20$3-$1-$2_$4";
#	print "$file\t$newFile\n";
	rename "$dir/$file", "$dir/$newFile" or die "rename failed $!\n";
    }
}
closedir DIR;
